/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.qrcode.QRCodeWriter;
import org.jsoup.Jsoup;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: javase - Dict
 * <p>Powered by webrx On 2021-12-16 20:04:50
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Dict {
    public static void main(String[] args) throws IOException, WriterException {
        String w = JOptionPane.showInputDialog("请输入词汇：");
        String u = String.format("http://www.youdao.com/w/eng/%s/#keyfrom=dict2.index", w);
        String t = Jsoup.connect(u).get().select("div[class=trans-container]").get(0).text();
        //JOptionPane.showMessageDialog(null, t);

        String txt = String.format("%s : %s", w, t);
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        var bm = new QRCodeWriter().encode(txt, BarcodeFormat.QR_CODE, 200, 200, hints);
        var i = MatrixToImageWriter.toBufferedImage(bm);
        var dst = String.format("c:/%s.png", w);
        ImageIO.write(i, "png", new FileOutputStream(dst));

        var run = Runtime.getRuntime();
        run.exec("cmd /k start " + dst);

    }
}
